package com.example.my_application

import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.baidu.calculatordemo.UnicodeTextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val textViews = listOf(
            findViewById<UnicodeTextView>(R.id.zerozero),
            findViewById<UnicodeTextView>(R.id.zero),
            findViewById<UnicodeTextView>(R.id.one),
            findViewById<UnicodeTextView>(R.id.two),
            findViewById<UnicodeTextView>(R.id.three),
            findViewById<UnicodeTextView>(R.id.four),
            findViewById<UnicodeTextView>(R.id.five),
            findViewById<UnicodeTextView>(R.id.six),
            findViewById<UnicodeTextView>(R.id.seven),
            findViewById<UnicodeTextView>(R.id.eight),
            findViewById<UnicodeTextView>(R.id.nine),
            findViewById<UnicodeTextView>(R.id.plus),
            findViewById<UnicodeTextView>(R.id.minus),
            findViewById<UnicodeTextView>(R.id.multiply),
            findViewById<UnicodeTextView>(R.id.device),
            //findViewById<UnicodeTextView>(R.id.equal),
            //findViewById<UnicodeTextView>(R.id.percent),
            findViewById<UnicodeTextView>(R.id.delete),
            findViewById<UnicodeTextView>(R.id.clear),
            findViewById<UnicodeTextView>(R.id.point),
        )
        var s=""
        var r:Double=0.0
        // Set click listeners
        var b:Char = ' '

        textViews.forEach { textView ->

            var process=findViewById<TextView>(R.id.tv_process)
            var result=findViewById<TextView>(R.id.tv_result)
            textView.setOnClickListener {
                if (s.isNotEmpty()) {
                    b = s[s.length - 1]
                }
                when (textView.id) {
                    R.id.clear -> {
                        s = "0"

                        process.text = s

                        result.text = "0"
                    }
                    R.id.zero, R.id.one, R.id.two, R.id.three, R.id.four, R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine -> {
                        s += textView.text
                    }
                    R.id.zerozero -> {
                        s += "00"
                    }
                    R.id.plus -> {
                        if(s.length==0){
                            s="0+"
                        }
                        if (b in "0123456789") {
                            s += "+"
                        } else {
                            s = s.substring(0, s.length - 1) + "+"
                        }
                    }
                    R.id.minus -> {
                        if(s.length==0||s=="0+"||s=="0*"||s=="0/"){
                            s="0-"
                        }
                        if (b in "0123456789") {
                            s += "-"
                        }
                    }
                    R.id.multiply -> {
                        if(s.length==0){
                            s="0*"
                        }
                        if (b in "0123456789") {
                            s += "*"
                        } else {
                            s = s.substring(0, s.length - 1) + "*"
                        }
                    }
                    R.id.device -> {
                        if(s.length==0){
                            s="0/"
                        }
                        if (b in "0123456789") {
                            s += "/"
                        } else {
                            s = s.substring(0, s.length - 1) + "/"
                        }
                    }
                    R.id.point -> {
                        if (b in "0123456789" && numbertrue(s)) {
                            s += "."
                        }
                    }
                    R.id.delete -> {
                        if(s.length==0){
                            s="0"
                        }else if(s.length==1){
                            s="0"
                        }else if (s.isNotEmpty()) {
                            s = s.substring(0, s.length - 1)
                        }
                    }
                }
                if(s[0]=='0'){

                    process.text=""
                    result.text ="0"
                    s=""
                    b=' '
                }else{
                        process.text = s
                        r = if (s.isNotEmpty()) caculate(s) else 0.0
                        if(s.length==1)
                        {
                            r=r-48
                        }
                        if(s.length==2&&(s[1]=='-'||s[1]=='+'||s[1]=='*'||s[1]=='/')){
                            r=r-48
                        }
                        result.text = r.toString()

                }

            }
        }
    }
}

fun caculate(s:String):Double{

    if(s.length==1){
        if(s[0] in "*/-+.")
        {return 0.0}

        return s[0].toDouble()
    }
    var s1=s
    if(s[s.length-1] in "+-*/."){
        s1=s.substring(0,s.length-1)
    }
    if(s1.length==1){
        if(s[0] in "*/-+.")
        {return 0.0}

        return s1[0].toDouble()
    }
    var numbers= splitExpression(s1)
    var i = 0
    while (i < numbers.size) {
        if (numbers[i] == "*") {
            numbers[i] = (numbers[i - 1].toDouble() * numbers[i + 1].toDouble()).toString()
            numbers[i - 1] = " "
            numbers[i + 1] = " "
            numbers = removeSpacesFromList(numbers)
            i = 0
        } else if (numbers[i] == "/") {
            numbers[i] = (numbers[i - 1].toDouble() / numbers[i + 1].toDouble()).toString()
            numbers[i - 1] = " "
            numbers[i + 1] = " "
            numbers = removeSpacesFromList(numbers)
            i = 0
        } else {
            i++
        }
    }

    // 处理加减法
    i = 0
    while (i < numbers.size) {
        if (numbers[i] == "+") {
            numbers[i] = (numbers[i - 1].toDouble() + numbers[i + 1].toDouble()).toString()
            numbers[i - 1] = " "
            numbers[i + 1] = " "
            numbers = removeSpacesFromList(numbers)
            i = 0
        } else if (numbers[i] == "-") {
            numbers[i] = (numbers[i - 1].toDouble() - numbers[i + 1].toDouble()).toString()
            numbers[i - 1] = " "
            numbers[i + 1] = " "
            numbers = removeSpacesFromList(numbers)
            i = 0
        } else {
            i++
        }
    }

    return numbers[0].toDouble()
}
fun numbertrue(s:String):Boolean{

    for(i in s.length-1 downTo 0){
        if(s[i]=='.'){
            return false
        }
        if(s[i] in "+-*/"){
            break
        }
    }
    return true
}
fun splitExpression(expression: String): MutableList<String> {
    // 正则表达式匹配数字（包括整数和小数）和运算符
    val regex = Regex("""\d+\.\d+|\d+|[-+*/]""")
    // 使用findAll方法找到所有匹配的子串
    return regex.findAll(expression).map { it.value }.toMutableList()
}
fun removeSpacesFromList(list: MutableList<String>): MutableList<String> {
    list.removeIf { it == " " }
    return list
}